/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcmus.fit.master.sevensea.sprite.ship;

import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.core.AbstractSprite;
import hcmus.fit.master.sevensea.core.Constant;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Point;

/**
 * 
 * @author hvp
 */
public abstract class WarShip extends Ship {
    public Cannon[] Cannons;
    public int ViewRange;

    public WarShip(Sea seamap, int x, int y) {
        super(seamap, x, y);
        ViewRange = 3;

        Cannons = new Cannon[2];
        Cannon cannon = new Cannon(getSeaMap(), X, Y);
        Cannons[0] = cannon;
        cannon = new Cannon(getSeaMap(), X, Y);
        Cannons[1] = cannon;
        // Cannons.Clear();
    }

    public WarShip(WarShip pirate) {
        super(pirate);
        Cannons = pirate.Cannons;
        ViewRange = pirate.ViewRange;
    }

    // Direction
    // 7 0 1
    // 6 2
    // 5 4 3
    public void Shoot(AbstractSprite[][] items) {
        List<Point> targets = GetDestination(items); // cần xem lại
        int i = 0;
        for (int j = 0; j < targets.size(); j++) {
            Point target = (Point) targets.get(j);
            if (target.x != X || target.y != Y) {
                Cannon cannon = Cannons[i];
                cannon.State = CannonState.Flying;
                cannon.InitPosition(X, Y);
                cannon.DestX = target.x;
                cannon.DestY = target.y;
                getSeaMap().items.add(cannon);
                getSeaMap().busy++;
                i++;
            }
        }
        if (i <= 0)
            getSeaMap().busy--;
        else {
            // Cannon.Sound.Play();
            // try{
            // Cannon.Sound.start();
            // }
            // catch(Exception ex){}
        }
    }

    public List<Point> GetDestination(AbstractSprite[][] map) {
        List<Point> rs = new ArrayList<Point>();
        int currentDirection = Status / 2;
        int i;
        switch (currentDirection) {
            case 0:
            case 4:
                for (i = 1; i < ViewRange + 1; i++) {
                    if (X + i >= Constant.Width) {
                        rs.add(new Point(X + i - 1, Y));
                        break;
                    }
                    if (map[Y][X + i] != null) {
                        if (map[Y][X + i].isShip()) {
                            rs.add(new Point(X + i, Y));
                            break;
                        }

                        if (map[Y][X + i].isIsland()) {
                            rs.add(new Point(X + i - 1, Y));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X + i - 1, Y));
                }

                for (i = 1; i < ViewRange + 1; i++) {
                    if (X - i < 0) {
                        rs.add(new Point(0, Y));
                        break;
                    }
                    if (map[Y][X - i] != null) {
                        if (map[Y][X - i].isShip()) {
                            rs.add(new Point(X - i, Y));
                            break;
                        }

                        if (map[Y][X - i].isIsland()) {
                            rs.add(new Point(X - i + 1, Y));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X - i + 1, Y));
                }
                break;
            case 1:
            case 5:
                for (i = 1; i < ViewRange + 1; i++) {
                    if (X + i >= Constant.Width || Y + i >= Constant.Height) {
                        rs.add(new Point(X + i - 1, Y + i - 1));
                        break;
                    }
                    if (map[Y + i][X + i] != null) {
                        if (map[Y + i][X + i].isShip()) {
                            rs.add(new Point(X + i, Y + i));
                            break;
                        }

                        if (map[Y + i][X + i].isIsland()) {
                            rs.add(new Point(X + i - 1, Y + i - 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X + i - 1, Y + i - 1));
                }

                for (i = 1; i < ViewRange + 1; i++) {
                    if (X - i < 0 || Y - i < 0) {
                        rs.add(new Point(X - i + 1, Y - i + 1));
                        break;
                    }
                    if (map[Y - i][X - i] != null) {
                        if (map[Y - i][X - i].isShip()) {
                            rs.add(new Point(X - i, Y - i));
                            break;
                        }

                        if (map[Y - i][X - i].isIsland()) {
                            rs.add(new Point(X - i + 1, Y - i + 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X - i + 1, Y - i + 1));
                }
                break;
            case 2:
            case 6:
                for (i = 1; i < ViewRange + 1; i++) {
                    if (Y + i >= Constant.Height) {
                        rs.add(new Point(X, Y + i - 1));
                        break;
                    }
                    if (map[Y + i][X] != null) {
                        if (map[Y + i][X].isShip()) {
                            rs.add(new Point(X, Y + i));
                            break;
                        }

                        if (map[Y + i][X].isIsland()) {
                            rs.add(new Point(X, Y + i - 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X, Y + i - 1));
                }

                for (i = 1; i < ViewRange + 1; i++) {
                    if (Y - i < 0) {
                        rs.add(new Point(X, Y - i + 1));
                        break;
                    }
                    if (map[Y - i][X] != null) {
                        if (map[Y - i][X].isShip()) {
                            rs.add(new Point(X, Y - i));
                            break;
                        }

                        if (map[Y - i][X].isIsland()) {
                            rs.add(new Point(X, Y - i + 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X, Y - i + 1));
                }
                break;
            case 3:
            case 7:
                for (i = 1; i < ViewRange + 1; i++) {
                    if (X + i >= Constant.Width || Y - i < 0) {
                        rs.add(new Point(X + i - 1, Y - i + 1));
                        break;
                    }
                    if (map[Y - i][X + i] != null) {
                        if (map[Y - i][X + i].isShip()) {
                            rs.add(new Point(X + i, Y - i));
                            break;
                        }

                        if (map[Y - i][X + i].isIsland()) {
                            rs.add(new Point(X + i - 1, Y - i + 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X + i - 1, Y - i + 1));
                }

                for (i = 1; i < ViewRange + 1; i++) {
                    if (X - i < 0 || Y + i >= Constant.Height) {
                        rs.add(new Point(X - i + 1, Y + i - 1));
                        break;
                    }
                    if (map[Y + i][X - i] != null) {
                        if (map[Y + i][X - i].isShip()) {
                            rs.add(new Point(X - i, Y + i));
                            break;
                        }

                        if (map[Y + i][X - i].isIsland()) {
                            rs.add(new Point(X - i + 1, Y + i - 1));
                            break;
                        }
                    }
                }
                if (i >= ViewRange + 1) {
                    rs.add(new Point(X - i + 1, Y + i - 1));
                }
                break;
        }
        return rs;
    }
}
